#!/usr/bin/env python3

from get_micro_data import *


#配置mongodb的信息
client = pymongo.MongoClient(MONGO_URL)
db = client[MONGO_DB]

class start_program(object):
    def __init__(self,university_name):
        self.fail_university_list = []
        self.university_name = university_name

    '''
    开始多线程获取数据
    '''
    def get_start_program(self):
        # fail_university_list = []
        #top:162
        list = [item * 1000 for item in range(300)]
        # fail_university_list.append(self.university_name)
        length = 0
        while length >= 0 and length < len(list):
            offset = list[length]
            micro_data = get_micro_data(self.university_name,str(offset))
            university_paper_data,status = micro_data.use_micro_interface()
            length += 1
            print("++++++++++++++++++++++",length)
            if status != 200:
                print("获取" + self.university_name + "的第" + str(offset) + "到" + str(offset + 1000) +"条数据失败")
                self.fail_university_list.append({self.university_name:offset})
                time.sleep(20)

            # elif len(university_paper_data) == 0 and status == 200:
            #     print("获取" + self.university_name + "的第" + str(offset) + "条数据长度为0，代表该学校的论文数量爬取完了!!!!!!")
            elif status == 200 and len(university_paper_data) != 0:
                print("获取" + self.university_name + "的第" + str(offset) + "到" + str(offset + 1000) +"条数据成功!!!!!!")
                # 先不插入mongod
                # save_into_mongod(university_paper_data,self.university_name)
                for item in university_paper_data:
                    db[self.university_name].save(item)
            else:
                length = -1



        return self.fail_university_list
